import 'package:flutter/material.dart';
import 'dart:math';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Material App',
//      theme: ThemeData(
//          primarySwatch: Colors.lightBlue,
//          scaffoldBackgroundColor: Colors.white),
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage>
    with SingleTickerProviderStateMixin {
//1.创建AnimationController
  AnimationController controller;
  Animation animation;
  Animation sizeAnimation;
  Animation colorAnimation;
  Animation opacityAnimation;
  Animation radiansAnimation;
 bool isBeginAnimation = false;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
//  1.1 初始化AnimationController
    controller = AnimationController(
      vsync: this,
      duration: Duration(seconds: 2),
//      lowerBound: 50,
//      upperBound: 150,
    );
//    1.2设置curved值
    animation = CurvedAnimation(parent: controller, curve: Curves.linear);
//    1.3设置size的tween
    sizeAnimation = Tween(begin: 10.0, end: 200.0).animate(controller);
//    1.4设置color的tween
    colorAnimation =
        ColorTween(begin: Colors.orange, end: Colors.purple).animate(controller);
//    1.5设置opacity的tween
    opacityAnimation = Tween(begin: 0.0, end: 1.0).animate(controller);
//    1.6设置radians的tween
    radiansAnimation = Tween(begin: 0.0, end: 2 * pi).animate(controller);
//    2.监听值的变化
//    controller.addListener(() {
//      setState(() {});
//    });
//    3.监听状态的改变
    controller.addStatusListener((status) {
      if (status == AnimationStatus.completed) {
        controller.reverse();
      } else if (status == AnimationStatus.dismissed) {
        controller.forward();
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          'Material App Bar',
          style: TextStyle(color: Colors.white),
        ),
      ),
      body: Center(
        child: AnimatedBuilder(
          animation: controller,
          builder: (context,child){
            return Opacity(
              opacity: isBeginAnimation ? opacityAnimation.value:1.0,
              child: Transform(
                transform: Matrix4.rotationZ(radiansAnimation.value),
                alignment: Alignment.center,
                child: Container(
                  width: sizeAnimation.value,
                  height: sizeAnimation.value,
                  color: colorAnimation.value,
                ),
              ),
            );
          },
        )
      ),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.play_arrow),
        onPressed: () {
          isBeginAnimation = true;
          if (controller.isAnimating) {
            controller.stop();
          } else if (controller.status == AnimationStatus.forward) {
            controller.forward();
          } else if (controller.status == AnimationStatus.reverse) {
            controller.reverse();
          } else {
            controller.forward();
          }
        },
      ),
    );
  }

  @override
  void dispose() {
    controller.dispose();
    // TODO: implement dispose
    super.dispose();
  }
}
